#include "hplcpumpsystemexternpage.h"
#include "modulemanager.h"
#include "modulehplcpump.h"
#include "modulehplcpumpparamiddefine.h"
#include "modulehplccmdid.h"
#include <QDebug>
#include <QCloseEvent>

HplcPumpSystemExternPage::HplcPumpSystemExternPage(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("Hplc Pump System Extern Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    ModuleHplcPump *mod = static_cast<ModuleHplcPump*>(moduleManager->find(ModuleID_HPLC_PumpA));
    if (mod == nullptr)
        return;
    qDebug() << mod->getName();
    connect(mod, &ModuleHplcPump::parseCompleted,
            this, &HplcPumpSystemExternPage::onParseCompleted, Qt::QueuedConnection);

    // 工作模式
    m_lbWorkMode = new QLabel(tr("WorkMode"));
    m_leWorkMode = new QLineEdit();
    m_pbSetWorkMode = new QPushButton(tr("Set"));
    connect(m_pbSetWorkMode, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_leWorkMode->text().toInt());
        mod->setProtocolCmd(hplc::PumpSystemCmdID_SetWorkMode, var);
    });
    m_pbGetWorkMode = new QPushButton(tr("Get"));
    connect(m_pbGetWorkMode, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::PumpSystemCmdID_GetWorkMode, var);
    });
    hblWorkMode = new QHBoxLayout();
    hblWorkMode->addWidget(m_lbWorkMode);
    hblWorkMode->addWidget(m_leWorkMode);
    hblWorkMode->addWidget(m_pbSetWorkMode);
    hblWorkMode->addWidget(m_pbGetWorkMode);

    // 泵头材质
    m_lbPumpMaterial = new QLabel(tr("PumpMaterial"));
    m_lePumpMaterial = new QLineEdit();
    m_pbSetPumpMaterial = new QPushButton(tr("Set"));
    connect(m_pbSetPumpMaterial, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_lePumpMaterial->text().toInt());
        mod->setProtocolCmd(hplc::PumpSystemCmdID_SetPumpMaterial, var);
    });
    m_pbGetPumpMaterial = new QPushButton(tr("Get"));
    connect(m_pbGetPumpMaterial, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::PumpSystemCmdID_GetPumpMaterial, var);
    });
    hblPumpMaterial = new QHBoxLayout();
    hblPumpMaterial->addWidget(m_lbPumpMaterial);
    hblPumpMaterial->addWidget(m_lePumpMaterial);
    hblPumpMaterial->addWidget(m_pbSetPumpMaterial);
    hblPumpMaterial->addWidget(m_pbGetPumpMaterial);

    // current rpm
    m_lbCurrentRpm = new QLabel(tr("CurrentRpm"));
    m_leCurrentRpm = new QLineEdit();
    m_pbSetCurrentRpm = new QPushButton(tr("Set"));
    connect(m_pbSetCurrentRpm, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_leCurrentRpm->text().toInt());
        mod->setProtocolCmd(hplc::PumpSystemCmdID_SetCurrentRpm, var);
    });
    m_pbGetCurrentRpm = new QPushButton(tr("Get"));
    connect(m_pbGetCurrentRpm, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::PumpSystemCmdID_GetCurrentRpm, var);
    });
    hblCurrentRpm = new QHBoxLayout();
    hblCurrentRpm->addWidget(m_lbCurrentRpm);
    hblCurrentRpm->addWidget(m_leCurrentRpm);
    hblCurrentRpm->addWidget(m_pbSetCurrentRpm);
    hblCurrentRpm->addWidget(m_pbGetCurrentRpm);

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(hblWorkMode);
    vbl->addLayout(hblPumpMaterial);
    vbl->addLayout(hblCurrentRpm);
    setLayout(vbl);
}

HplcPumpSystemExternPage::~HplcPumpSystemExternPage()
{

}

void HplcPumpSystemExternPage::closeEvent(QCloseEvent *event)
{
    disconnect();
    event->accept();
}

void HplcPumpSystemExternPage::onParseCompleted(int id, const QVariant &var)
{
    switch (id)
    {
        case hplc::PumpSystemCmdID_SetWorkMode:
        {
            quint8 mode = var.toUInt();
            m_leWorkMode->setText(QString::number(mode, 2));
        }
        break;
        case hplc::PumpSystemCmdID_SetPumpMaterial:
        {
            quint8 material = var.toUInt();
            m_lePumpMaterial->setText(QString::number(material));
        }
        break;
        case hplc::PumpSystemCmdID_SetCurrentRpm:
        {
            float currentRpm = var.toFloat();
            m_leCurrentRpm->setText(QString::number(currentRpm));
        }
        break;
        default:
        break;
    }
}
